<?php
set_time_limit(60);
class CommonAction extends Action {
	protected $webConfig;
	protected $memberInfo;
	protected $modelObject;
	protected $modelViewObject;
	
	//index操作方法
    public function _before_index(){
		$modelName = strtolower(MODULE_NAME);
		$actionName = strtolower(ACTION_NAME);
		$ext = __EXT__;
		
		if(in_array($modelName, array('index', 'search'))){
			return true;
		}else if($modelName == 'sitemap'){
			if($ext != 'xml'){
				$this->go404();
			}
			return true;
		}
		
		
		$requestUrl = urldecode($_SERVER['REQUEST_URI']);
		
		$requestUrlArr = explode('/', $requestUrl);
		unset($requestUrlArr[0]);
		$requestUrlCount = count($requestUrlArr);
		
		$urlArr1 = array('/websites','/news','/top','/member','/category', '/addurl');
		$urlArr2 = array('/websites/','/news/','/top/','/member/','/category/', '/addurl/');
		
		if(in_array($requestUrl, $urlArr1)){
			send_http_status(301);
			header('Location:'.$requestUrl.'/');
			exit;
		}else if(!in_array($requestUrl, $urlArr2) && (substr($requestUrl,-5) != '.html' && $requestUrlCount > 1)){
			$this->go404();	
		}
    }
	//show操作方法
    public function _before_show(){
        $ext = __EXT__;
		if($ext != 'html'){
			$this->go404();	
		}
    }
	
    public function __construct(){
		parent::__construct();
		header('Content-Type:text/html; charset='.C('DEFAULT_CHARSET'));
		
		//获取网站配置
		$this->webConfig = D('WebConfig')->getWebConfig();
		$this->webConfig['host_url'] = 'http://'.$this->webConfig['web_url'].'/';
		$this->assign('webConfig', $this->webConfig);

		if(!($this->memberInfo = session('memberInfo'))){
			$this->memberInfo['id'] = 0;
		}else{
			$this->assign('memberInfo', $this->memberInfo);	
		}
		
		//数据统计
		if(!($siteInfoCount = S('siteInfoCount'))){
			$siteInfoCount = array();
			$siteInfoCount['time'] = NOW_TIME;
			$siteInfoCount['category'] = M('WebsitesCate')->where('status = 1')->count(); //分类数
			$siteInfoCount['websites'] = M('Websites')->where('status = 1')->count();  //收录网站数
			$siteInfoCount['webapply'] = M('Webapply')->count();  //待审核网站数
			
			S('siteInfoCount', $siteInfoCount, 3600*24); //每天更新一次
		}
		$this->assign('siteInfoCount', $siteInfoCount);	
		
		//设置导航
		$this->getNav();
		
		//网站点入
		if(isset($_SERVER['HTTP_REFERER'])){
			$refurl = strtolower($_SERVER['HTTP_REFERER']);
			if (preg_match("/^http(s)?:\/\/?([^\/]+)/i", $refurl, $matches)) {
				$domain = $matches[2];
				if (!empty($domain) && $domain != strtolower($_SERVER['HTTP_HOST'])) {
					M('Websites')->where(array('domain'=>$domain))->setField(array('instat'=>array('exp','instat+1'), 'in_time'=>NOW_TIME));
				}				
			}
		}
	}
	
	protected function setSeo($info = array(), $code = '', $nowPage = 1){
		if(!empty($code)){
			if(!($seoInfo = F('seo_'.$code))){
				$seoInfo = M('Seo')->where(array('code'=>$code))->find();
				F('seo_'.$code, $seoInfo);
			}
			
			if(empty($info['name']) && empty($info['title'])){//不存在原始信息，那么把seoInfo的信息放进来
				$info['title']       = $seoInfo['title'];	
			}
			if(empty($info['keywords'])){
				$info['keywords']    = $seoInfo['keywords'];
			}
			if(empty($info['description'])){
				$info['description'] = $seoInfo['description'];	
			}
		}
		
		if($seoInfo['show_page'] == 1 && $nowPage > 1){
			$showPage = '第'.$nowPage.'页';	
		}
		$title = empty($info['title']) ? $info['name'] : $info['title'];
		$this->assign('title', $title . $showPage . ' - '. $this->webConfig['web_name']);	
		$this->assign('shortTitle', $title);	

		if(!empty($info['keywords'])){
			$this->assign('keywords', $info['keywords']);	
		}
		
		if(!empty($info['description'])){
			$this->assign('description', $info['description']);	
		}
	}
	
	/**
	* 跳转到404
	*/
	protected function go404(){
	   send_http_status(404);
	   $this->setSeo(array('title'=>'页面不存在或已删除'));
	   $this->display('Public:404');
	   exit;
	}
	
	/*二维码生成
	<img src="{:U('/common/qrcode')}" />
	*/
	public function qrcode($url){
		if(!empty($url)){
			Vendor('phpqrcode.phpqrcode');
			QRcode::png($url, false, 'H', 6, 1);
		}
	}
	
	/**
	* 公用收藏
	*/
	public function good(){
		$types = I('types', '', 'trim');
		$targetId = I('id', 0, 'intval');
		$result = array();
		//必须是ajax请求才能生效
		if(IS_AJAX && $targetId > 0 && in_array($types, C('SHARE_ACTION'))){
			if($this->memberInfo['id'] == 0){
				$result['error'] = 0;
				$result['message'] = '请先登录';
				$this->ajaxReturn($result);
			}
			$data = array();
			$data['types'] = $types;
			$data['target_id'] = $targetId;
			$data['ip'] = get_client_ip();
			$data['add_time'] = NOW_TIME;
			$data['good'] = 1;
			$data['member_id'] = $this->memberInfo['id'];
			$goodBad = M('goodBad');
			if($goodBad->getInfo(array('types'=>$types,'member_id'=>$data['member_id'],'target_id'=>$targetId))){
				$result['error'] = 0;
				$result['message'] = '你已经收藏过了';
				$this->ajaxReturn($result);
			}else if($goodBad->add($data)){
				//给原始数据表加个冗余
				switch($types){
					case 'websites':
						$modelObject = M('Websites');
					break;	
					case 'news':
						$modelObject = M('News');
					break;
				}
				if(isset($modelObject)){
					$resultCount = $modelObject->where(array('id'=>$targetId))->setInc('like_count');
					$result['error'] = 1;
				}else{
					$result['error'] = 0;
					$result['message'] = '参数有误';
				}
				$this->ajaxReturn($result);
			}else{
				$result['error'] = 0;
				$result['message'] = '参数有误';
				$this->ajaxReturn($result);
			}
		}else{
			$result['error'] = 0;
			$this->ajaxReturn($result);
		}	
	}
	
	public function _empty(){
		$this->go404();
	}
	
	//支付方式异步处理接口
	public function notify(){
		$type = I('type', '');
		switch($type){
			case 'alipay':
				Vendor('Payment.alipay.notify_url');
			break;
		}
	}
	public function payment(){
		$type = I('type', '');
		switch($type){
			case 'alipay':
				$result = include(THINK_PATH.'/Extend/Vendor/Payment/alipay/return_url.php');
				if($result == "success"){
					$this->success('付款成功,正在跳转到订单列表', U('/member/recharge'));	
				}else{
					$this->success('付款失败,如果您确定扣款成功,请联系管理员!', U('/member/recharge'));	
				}
			break;
			default:
				$this->go404();	
		}
	}
	
	/**
	* 构造导航的树形结构
	* @param activeUrls array 导航需要选中的url列表
	* @return array
	*/
	protected function getNav($activeUrls = array()){
		//导航处理start
		$columnObject = D('Column');
		if(!($nav = F('nav'))){
			$nav = $columnObject->getAllCate();
			F('nav', $nav);		
		}
		
		$this->assign('nav', $columnObject->getCateTree($nav, $activeUrls) );	
		//导航处理end			
	}
}//end class